<template>
    <show-modal :title="title" :loading="loading" width="500px" @cancel="cancel" @submit="submit">
        <a-form-model ref="form" :model="form" :rules="rules" v-bind="layout">
            <a-form-model-item label="新密码" prop="password">
                <a-input v-model.trim="form.password" placeholder="请填写用户新密码" type="text" />
            </a-form-model-item>
            <a-form-model-item label="确认密码" prop="repassword">
                <a-input v-model.trim="form.repassword" placeholder="请确认用户新密码" type="text" />
            </a-form-model-item>
        </a-form-model>
    </show-modal>
</template>

<script>
import { addInfo, updateInfo } from '@/api/system/user';
import { mapState } from 'vuex';
import { getCache } from '@/utils/session';

export default {
    name: 'ResetPassword',
    props: {
        info: {
            type: Object,
            default: () => {}
        }
    },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.form.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            loading: false,
            rules: {
                password: [
                    { required: true, message: '请输入用户密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '用户密码长度6-20位', trigger: 'blur' }
                ],
                repassword: [{ validator: validatePass, trigger: 'blur', required: true }]
            },
            title: '重置用户密码',
            form: {
                userName: '',
                userPassword: '123456',
                telephone: '',
                roleName: undefined,
                deptName: undefined,
                status: 1
            },
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            }
        };
    },
    computed: {
        ...mapState({
            accountInfo: state => state.user.accountInfo
        })
    },
    created(){
        
    },
    methods: {
        submit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        let res = null;
                        this.loading = true;
                        if (this.form.userId) {
                            delete this.form.userPassword;
                            res = await updateInfo(this.form);
                        } else {
                            res = await addInfo(this.form);
                        }
                        if (res.code == 200) {
                            this.$message.success('操作成功!');
                            if(this.accountInfo.userId==this.form.userId){
                                this.$store.dispatch('user/logout');
                                let tenant = getCache('tenant') || 'master';
                                this.$router.replace({
                                    path: `/${tenant}/login`
                                });
                            }else{
                                this.$emit('submit');
                            }
                        }
                    } finally {
                        this.loading = false;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //取消
        cancel() {
            this.$emit('cancel');
        }
    }
};
</script>

